<?php
namespace app\api\controller\wdsxh;
use app\admin\model\wdsxh\Article as Model;
use app\admin\model\wdsxh\ArticleCat;
/**
 * 文章管理
 */
class Article extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function cat(){
        $pid=$this->request->param('pid',0);
        $list=ArticleCat::where('status','1')
            ->where('pid',$pid)
            ->order('weigh desc')
            ->field('id,pid,name')
            ->select();
        $this->success($list);
    }
    public function lists(){
        $cat_id=$this->request->param('cat_id',0);
        if(empty($cat_id)){
            $this->error('请求参数错误');
        }
        $map=array(
            'status'=>'1',
            'cat_id'=>$cat_id
        );
        $cat=ArticleCat::where('id',$cat_id)->field('pid')->find();
        $arr=array();
        if($cat){
            if($cat['pid'] == 0){
                $sub=ArticleCat::where('pid',$cat_id)->field('id')->select();
                if($sub){
                    $sub=collection($sub)->toArray();
                    if(count($sub) > 0){
                        foreach ($sub as $row){
                            $arr[]=$row['id'];
                        }
                        $map['cat_id']=['in',$arr];
                    }
                }
            }
        }
        $param = $this->request->param();

        $list=Model::where($map)
            ->order('weigh desc,createtime desc')
            ->page($param['page'],$param['limit'])
            ->select();
        if($list){
            $list=collection($list)->toArray();
            foreach ($list as &$row){
                $row['image']=wdsxh_full_url($row['image']);
                $row['createtime']=date('Y-m-d H:i',$row['createtime']);
            }
        }
        $this->success($list);
    }

    public function lists_new(){
        $cat_id=$this->request->param('cat_id',0);
        if(empty($cat_id)){
            $this->error('请求参数错误');
        }
        $map=array(
            'status'=>'1',
            'cat_id'=>$cat_id
        );
        $cat=ArticleCat::where('id',$cat_id)->field('pid')->find();
        $arr=array();
        if($cat){
            if($cat['pid'] == 0){
                $sub=ArticleCat::where('pid',$cat_id)->field('id')->select();
                if($sub){
                    $sub=collection($sub)->toArray();
                    if(count($sub) > 0){
                        foreach ($sub as $row){
                            $arr[]=$row['id'];
                        }
                        $map['cat_id']=['in',$arr];
                    }
                }
            }
        }
        $param = $this->request->param();

        $list=Model::where($map)
            ->order('weigh desc,createtime desc')
            ->page($param['page'],$param['limit'])
            ->select();
        if($list){
            $list=collection($list)->toArray();
            foreach ($list as &$row){
                $row['image']=wdsxh_full_url($row['image']);
                $row['createtime']=date('Y-m-d H:i',$row['createtime']);
            }
        }
        $count=Model::where($map)->count();
        $this->success(['list'=>$list,'count'=>$count]);
    }

    public function detail(){
        $id=$this->request->param('id',0);
        $data=Model::where('status','1')
            ->where('id',$id)
            ->find();
        if($data){
            $data['read_num']+=1;
            $data->save();
            $data=$data->toArray();
            $data['image']=wdsxh_full_url($data['image']);
            $data['createtime']=date('Y-m-d H:i',$data['createtime']);
        }
        $this->success($data);
    }
}